const { pool } = require('./config/db');

async function checkUsersTable() {
  try {
    // 检查users表是否存在
    const [tables] = await pool.execute(`
      SELECT table_name 
      FROM information_schema.tables 
      WHERE table_schema = 'aizaijia' AND table_name = 'users'
    `);
    
    console.log('Users表存在检查:', tables.length > 0 ? '存在' : '不存在');
    
    if (tables.length > 0) {
      // 查询users表的所有数据
      const [users] = await pool.execute('SELECT * FROM users');
      console.log('Users表中的用户数据:', users);
    }
    
    // 检查goods表的存在和数据
    const [goodsTables] = await pool.execute(`
      SELECT table_name 
      FROM information_schema.tables 
      WHERE table_schema = 'aizaijia' AND table_name = 'goods'
    `);
    
    console.log('Goods表存在检查:', goodsTables.length > 0 ? '存在' : '不存在');
    
    if (goodsTables.length > 0) {
      const [goods] = await pool.execute('SELECT * FROM goods');
      console.log('Goods表中的商品数据:', goods);
    }
    
    // 检查orders表的外键约束
    const [fkConstraints] = await pool.execute(`
      SELECT 
        constraint_name, 
        referenced_table_name, 
        referenced_column_name 
      FROM 
        information_schema.key_column_usage 
      WHERE 
        table_schema = 'aizaijia' AND 
        table_name = 'orders' AND 
        referenced_table_name IS NOT NULL
    `);
    
    console.log('Orders表的外键约束:', fkConstraints);
    
  } catch (error) {
    console.error('检查数据库时出错:', error);
  } finally {
    pool.end();
  }
}

checkUsersTable();